package zju.ccnt.fileserverProxy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import zju.ccnt.fileserverProxy.repository.FileServerRepository;
import zju.ccnt.fileserverProxy.repository.FileUploadRepository;
import zju.ccnt.rest.api.ApiCreateResource;

import java.util.Set;

/**
 * Created by zha on 14/11/28.
 */
@Service(value = "fileServerProxy")
public class FileServerProxy {

    private FileServerLoadBalance serverLoadBalance;
    private FileServerRepository serverRepository;
    private FileUploadRepository uploadRepository;

//    //商家对应资源的等待上传信息
//    private ConcurrentHashMap<String, ApiCreateResource> uploadInitiator;
//    //某商家对应资源的试图上传者列表
//    private ConcurrentHashMap<String, Set<String>> uploaders;
//    //某等待上传中的物理资源对应的商家资源表
//    private ConcurrentHashMap<String, Set<String>> resource2business;

    @Autowired
    public FileServerProxy(@Qualifier("scoreStrategyFileServerLoadBalance") FileServerLoadBalance serverLoadBalance,
                           FileServerRepository serverRepository,
                           FileUploadRepository uploadRepository){
        this.serverLoadBalance = serverLoadBalance;
        this.serverRepository = serverRepository;
//        this.uploadInitiator = new ConcurrentHashMap<String, ApiCreateResource>();
//        this.uploaders = new ConcurrentHashMap<String, Set<String>>();
//        this.resource2business = new ConcurrentHashMap<String, Set<String>>();
        this.uploadRepository = uploadRepository;
    }

    /**
     * 为需上传的文件选择最优的文件服务器
     * @param size
     *        需要上传的文件大小
     * @return
     *        文件服务器的上传URL
     */
    public String selectFileServer(int size){
        return this.serverLoadBalance.selectFileServer(size);
    }

    /**
     * 获取文件现在正在上传的服务器上传地址
     * @param hashValue
     * @return
     */
    public String getFileUploadUrl(String hashValue){
       return (String)uploadRepository.getFileUploadDetail(hashValue).get("Location");
    }

    private String buildResourceBusinessKey(String hashValue, String businessId){
        return hashValue + ":" + businessId;
    }


    /**
     * 正在等待用户开始上传的文件
     * @param uploadInfo
     * @return
     */
    public void addWaitingForPostUpload(String hashValue, String businessId, ApiCreateResource uploadInfo, String uploader){
        addWaitingForUploaded(hashValue, businessId, uploader);
        uploadRepository.addResourceWaitingForPost(hashValue, businessId, uploadInfo);
    }

    /**
     *  等待该用户继续上传
     * @param hashValue
     * @param uploader
     */
    public void addWaitingForUploaded(String hashValue, String businessId, String uploader){
        uploadRepository.addResourceToBusiness(hashValue, businessId);
        uploadRepository.addAttempUploaders(hashValue, businessId, uploader);
    }

    /**
     * 上传完成后，该资源对应的上传者缓存信息清除
     * @param hashValue
     */
    public void actionOnUploaded(String hashValue){
        uploadRepository.uploadedResource(hashValue);
    }

    public Set<String> getSuccessUploaders(String hashValue){
        return uploadRepository.getSuccessUploaders(hashValue);
    }

    public Set<String> getAttempUploaders(String hashValue, String businessId){
        return uploadRepository.getAttempUploaders(hashValue, businessId);
    }

    public ApiCreateResource getUploadInitial(String hashValue, String businessId){
        return uploadRepository.getInitialPostInfo(hashValue, businessId);
    }


    public ApiCreateResource removeUploadInitial(String hashValue, String businessId){
        return uploadRepository.removeResourceWaitingForPost(hashValue, businessId);
    }

    /**
     * 获取试图上传resource到的目标商家列表
     * @param hashValue
     * @return
     */
    public Set<String> getAttempBusiness(String hashValue){
        return uploadRepository.getBusinessIds(hashValue);
    }
}
